package com.night.card.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.night.card.mapper.CardOutMapper;
import com.night.card.service.interfs.CardOutService;
import com.night.common.entity.card.CardOut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CardOutServiceImp
 * @Description: 消费记录service层实现
 * @author: CHX42
 * @date: 2022/10/21-9:29
 * @version: v1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
@SuppressWarnings("all")//压制一切警告
public class CardOutServiceImp implements CardOutService {
    @Value("${ss.card.pageSize}")
    private int pageSize;

    @Resource
    private CardOutMapper cardOutMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public PageInfo<CardOut> getAll() {
        String pageName = "card_out_all";
        PageHelper.startPage(1, pageSize);
        List<CardOut> redisCardOutList = (List<CardOut>) redisTemplate.opsForValue().get(pageName);
        if (redisCardOutList != null)
            return new PageInfo<>(redisCardOutList);
        PageHelper.startPage(1, pageSize);
        List<CardOut> cardOutList = cardOutMapper.select(new CardOut());
        redisTemplate.opsForValue().set(pageName, cardOutList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardOutList);
    }

    @Override
    public PageInfo<CardOut> getBy(PageInfo<CardOut> pageInfo) {
        String pageName = "card_out_list_" + pageInfo.getPageNum() + "_" + pageInfo.getPageSize();
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<CardOut> redisCardOutList = (List<CardOut>) redisTemplate.opsForValue().get(pageName);
        if (redisCardOutList != null)
            return new PageInfo<>(redisCardOutList);
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<CardOut> cardOutList = cardOutMapper.select(pageInfo.getList().get(0));
        redisTemplate.opsForValue().set(pageName, cardOutList, 30, TimeUnit.SECONDS);
        return new PageInfo<>(cardOutList);
    }
}
